package tij4.concurrent.source;

/*
    演示了把一个非保护类型的类，在其他类的保护和控制下，
    应用于多线程的环境
 */

import org.omg.CORBA.INITIALIZE;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class Pair {
    private int x, y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public Pair() {
        this(0,0);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
    public void incrementX(){
        x++;
    }
    public void incrementY(){
        y++;
    }

    @Override
    public String toString() {
        return "x: " + x + ", y: " + y + ". ";
    }

    public class PairValuesNotEqualException extends RuntimeException {
        public PairValuesNotEqualException() {
            super("Pair values not equal: " + Pair.this);
        }
    }
    public void checkState() {
        if (x != y) {
            throw new PairValuesNotEqualException();
        }
    }
}

abstract class PairManager {
    AtomicInteger checkCount = new AtomicInteger(0);
    protected Pair p = new Pair();
    private List<Pair> storage = Collections.synchronizedList(new ArrayList<Pair>());

    public synchronized Pair getPair() {
        return new Pair(p.getX(), p.getY());
    }
    protected void store(Pair p) {
        storage.add(p);
        try {
            TimeUnit.MILLISECONDS.sleep(50);
        }catch (InterruptedException e){

        }
    }

    public abstract void increment();
}

class PairManager1 extends PairManager {
    public synchronized void increment() {
        p.incrementX();
        p.incrementY();
        store(getPair());
    }
}

class PairManager2 extends PairManager {
    public void increment() {
        Pair temp;
        synchronized (this) {
            p.incrementX();
            p.incrementY();
            temp = getPair();
        }
        store(temp);
    }
}

class PairManipulator implements Runnable {
    private PairManager pairManager;

    public PairManipulator(PairManager pairManager) {
        this.pairManager = pairManager;
    }

    @Override
    public void run() {
        while (true) {
            pairManager.increment();
        }
    }

    @Override
    public String toString() {
        return "Pair: " + pairManager.getPair() + " checkCount = " + pairManager.checkCount.get();
    }
}

class PairCheck implements Runnable {
    private PairManager pairManager;

    public PairCheck(PairManager pairManager) {
        this.pairManager = pairManager;
    }

    @Override
    public void run() {
        while (true) {
            pairManager.checkCount.incrementAndGet();
            pairManager.getPair().checkState();
        }
    }
}



public class CriticalSection {

    static void testApproaches(PairManager pairManager1, PairManager pairManager2) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        PairManipulator
                pairManipulator1 = new PairManipulator(pairManager1),
                pairManipulator2 = new PairManipulator(pairManager2);
        PairCheck
                pairCheck1 = new PairCheck(pairManager1),
                pairCheck2 = new PairCheck(pairManager2);
        executorService.execute(pairManipulator1);
        executorService.execute(pairManipulator2);
        executorService.execute(pairCheck1);
        executorService.execute(pairCheck2);
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        }catch (InterruptedException e) {
            System.out.println("Sleep interrupted");
        }
        System.out.println("pairManipulator1: " + pairManipulator1
                + "\npairManipulator2: " + pairManipulator2);
        System.exit(0);
    }

    public static void main(String[] args) {
        PairManager1 pairManager1 = new PairManager1();
        PairManager2 pairManager2 = new PairManager2();
        testApproaches(pairManager1, pairManager2);
    }

}
